package net.huashitong.Indicators.service;

import com.sirdc.modules.sys.service.RedisService;
import com.sirdc.modules.sys.util.RedisUtils;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Indicators.dataBean.IndexDataDTO;
import net.huashitong.Indicators.dataBean.IndexDto;
import net.huashitong.Indicators.entity.IndexData;
import net.huashitong.Indicators.entity.OneIndex;
import net.huashitong.Indicators.entity.UserInfo;
import net.huashitong.until.Until;
import org.hibernate.validator.constraints.br.CNPJ;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.Collator;
import java.util.*;

import static java.lang.Integer.parseInt;
import static net.huashitong.Indicators.dataBean.StaticIndicators.*;
import static net.huashitong.Indicators.utils.DataFormateUntil.dataFormat;
import static net.huashitong.Indicators.utils.DataFormateUntil.dataFormat1;
import static net.huashitong.Indicators.utils.DataFormateUntil.kexuejishu;
import static net.huashitong.until.SerializationDefine.List2Str;
import static net.huashitong.until.SerializationDefine.Str2List;
import static net.huashitong.until.Until.YUEDU_9DI;

@Service
public class AppApiService {
    @Autowired
    private OneIndexService oneservice;
    @Autowired
    private IndexDataService indexService;

    @Autowired
    private UserPressService userPressService;//权限服务

    @Autowired
    private RedisService redis;

    @Autowired
    private TwoIndexService twoService;
    @Autowired
    private ThreeIndexService threeService;


    protected Logger log = LoggerFactory.getLogger(AppApiService.class);


    //public void

    /**
     * app新目录
     *
     * @return
     */
    public List<Map<String, Object>> getHomeIndex(String openId, String time) {

        //单位map
//        Map<String, String> unitMap;
        Map<String, String> hideCodes = userPressService.getHideCodeList(openId);
        String datetime;
        if (ObjectUtils.isNotBlank(time)) {
            datetime = time;
        } else {
            //主目录是月度数据， codeType 为 Y
            datetime = indexService.getNewCurrentDate("Y", "E");
        }
        IndexData data;
        String areaType = "E";
        List<Map<String, Object>> listmap = new ArrayList<>();
        List<Map> twoIndexlist = new ArrayList<>();//子节点
        Map<String, Object> monthdata;
        List<IndexDataDTO> ranks;
        String rank = "-";
        Map<String, Object> onemap = new HashMap<>();
        String code_id;

        //GDP、一产、二产、三产调整为季度数据
        String quarterTime = dealWithQuarterTime(datetime);
        String quarterTitle = quarterTitleNameByTime(datetime);
        code_id = GDP_A;
        data = indexService.getIndexData(code_id, quarterTime, "GX350200", "J", areaType);
        monthdata = savedatas(code_id, "GX350200", "J", datetime.substring(0, 4), areaType);
        ranks = indexService.getRank(code_id, quarterTime, "GX350200",
                "J", areaType, null, true);//获取排名（增长值   含平潭）
        if (ObjectUtils.isNotBlank(ranks)) {
            rank = ranks.get(0).getRanks() + "";
        }
        onemap.put("monhtRatio", monthdata.get("list"));
        onemap.put("maxRatio", monthdata.get("max"));
        onemap.put("indexCode", code_id);
        onemap.put("areaType", areaType);
        onemap.put("codeType", "J");
        onemap.put("indexName", "地区生产总值（GDP）\n" + quarterTitle);
        onemap.put("totalValue", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getCumulativeValue()) ? "-" : dataFormat(data.getCumulativeValue()));
        onemap.put("ratio", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getCumulativeRatio()) ? "-" : dataFormat1(data.getCumulativeRatio()));
        onemap.put("ranks", rank);
        onemap.put("areaCode", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getAreaCode()) ? "GX350200" : data.getAreaCode());
        onemap.put("datetime", datetime);
        onemap.put("totalValueUnit", "亿元");    //单位
        if ("0".equals(onemap.get("totalValue")) && "0".equals(onemap.get("ratio"))) {
            onemap.put("ranks", "0");
        }
        Map<String, Object> child1 = new HashMap<>();

        //一产GDP_B
        code_id = GDP_B;
        data = indexService.getIndexData(code_id, quarterTime, "GX350200", "J", areaType);
        monthdata = savedatas(code_id, "GX350200", "J", datetime.substring(0, 4), areaType);
        ranks = indexService.getRank(code_id, datetime, "GX350200",
                "J", areaType, null, true);//获取排名（增长值   含平潭）
        if (ObjectUtils.isNotBlank(ranks)) {
            rank = ranks.get(0).getRanks() + "";
        }
        child1.put("monhtRatio", monthdata.get("list"));
        child1.put("maxRatio", monthdata.get("max"));
        child1.put("indexCode", code_id);
        child1.put("areaType", areaType);
        child1.put("codeType", "J");
        child1.put("indexName", "第一产业增加值\n" + quarterTitle);
        child1.put("totalValue", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getCumulativeValue()) ? "-" : dataFormat(data.getCumulativeValue()));
        child1.put("ratio", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getCumulativeRatio()) ? "-" : dataFormat1(data.getCumulativeRatio()));
        child1.put("ranks", rank);
        child1.put("datetime", datetime);
        child1.put("areaCode", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getAreaCode()) ? "GX350200" : data.getAreaCode());
        child1.put("totalValueUnit", "亿元");    //单位
        //如果总量为0，涨幅也为0则排名返回"-"
        if ("0".equals(child1.get("totalValue")) && "0".equals(child1.get("ratio"))) {
            child1.put("ranks", "-");
        }

        twoIndexlist.add(child1);

        //二产GDP_C
        code_id = GDP_C;
        data = indexService.getIndexData(code_id, quarterTime, "GX350200", "J", areaType);
        Map<String, Object> child2 = new HashMap<>();
        monthdata = savedatas(code_id, "GX350200", "J", datetime.substring(0, 4), areaType);
        ranks = indexService.getRank(code_id, datetime, "GX350200",
                "J", areaType, null, true);//获取排名（增长值   含平潭）
        if (ObjectUtils.isNotBlank(ranks)) {
            rank = ranks.get(0).getRanks() + "";
        }
        child2.put("monhtRatio", monthdata.get("list"));
        child2.put("maxRatio", monthdata.get("max"));
        child2.put("indexCode", code_id);
        child2.put("areaType", areaType);
        child2.put("codeType", "J");
        child2.put("indexName", "第二产业增加值\n" + quarterTitle);
        child2.put("totalValue", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getCumulativeValue()) ? "-" : dataFormat(data.getCumulativeValue()));
        child2.put("ratio", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getCumulativeRatio()) ? "-" : dataFormat1(data.getCumulativeRatio()));
        child2.put("ranks", rank);
        child2.put("datetime", datetime);
        child2.put("areaCode", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getAreaCode()) ? "GX350200" : data.getAreaCode());
        child2.put("totalValueUnit", "亿元");    //单位
        //如果总量为0，涨幅也为0则排名返回"-"
        if ("0".equals(child2.get("totalValue")) && "0".equals(child2.get("ratio"))) {
            child2.put("ranks", "-");
        }

        twoIndexlist.add(child2);

        //三产GDP_D
        code_id = GDP_D;
        data = indexService.getIndexData(code_id, quarterTime, "GX350200", "J", areaType);
        Map<String, Object> child3 = new HashMap<>();
        monthdata = savedatas(code_id, "GX350200", "J", datetime.substring(0, 4), areaType);
        ranks = indexService.getRank(code_id, datetime, "GX350200",
                "J", areaType, null, true);//获取排名（增长值   含平潭）
        if (ObjectUtils.isNotBlank(ranks)) {
            rank = ranks.get(0).getRanks() + "";
        }
        child3.put("monhtRatio", monthdata.get("list"));
        child3.put("maxRatio", monthdata.get("max"));
        child3.put("indexCode", code_id);
        child3.put("areaType", areaType);
        child3.put("codeType", "J");
        child3.put("indexName", "第三产业增加值\n" + quarterTitle);
        child3.put("totalValue", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getCumulativeValue()) ? "-" : dataFormat(data.getCumulativeValue()));
        child3.put("ratio", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getCumulativeRatio()) ? "-" : dataFormat1(data.getCumulativeRatio()));
        child3.put("ranks", rank);
        child3.put("datetime", datetime);
        child3.put("areaCode", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getAreaCode()) ? "GX350200" : data.getAreaCode());
        child3.put("totalValueUnit", "亿元");    //单位
        //如果总量为0，涨幅也为0则排名返回"-"
        if ("0".equals(child3.get("totalValue")) && "0".equals(child3.get("ratio"))) {
            child3.put("ranks", "-");
        }

        twoIndexlist.add(child3);

        onemap.put("childData", twoIndexlist);
        if (StringUtils.isBlank(hideCodes.get(GDP_A))) {//含有不可看指标 没有权限就不让看了
            listmap.add(onemap);
        }


        //--------------------------规模以上工业增加值 GMYSGY_ZJZ--------------------------
        code_id = GMYSGY_ZJZ;
        onemap = new HashMap<>();
        data = indexService.getIndexData(code_id, datetime, "GX350200", "Y", areaType);
        monthdata = savedatas(code_id, "GX350200", "Y", datetime.substring(0, 4), areaType);
        ranks = indexService.getRank(code_id, datetime, "GX350200",
                "Y", areaType, null, true);//获取排名（增长值   含平潭）
        if (ObjectUtils.isNotBlank(ranks)) {
            rank = ranks.get(0).getRanks() + "";
        }
        onemap.put("monhtRatio", monthdata.get("list"));
        onemap.put("maxRatio", monthdata.get("max"));
        onemap.put("indexCode", code_id);
        onemap.put("areaType", areaType);
        onemap.put("codeType", "Y");
        onemap.put("indexName", "规模以上工业增加值");
        onemap.put("totalValue", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getCumulativeValue()) ? "0" : data.getCumulativeValue());
        onemap.put("ratio", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getCumulativeRatio()) ? "0" : dataFormat1(data.getCumulativeRatio()));
        onemap.put("ranks", rank);
        onemap.put("datetime", datetime);
        onemap.put("areaCode", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getAreaCode()) ? "GX350200" : data.getAreaCode());
        onemap.put("totalValueUnit", "");    //单位

        if (StringUtils.isBlank(hideCodes.get(GMYSGY_ZJZ))) {// 没有权限就不让看了
            listmap.add(onemap);
        }

        //------------------------------GMYSGY_CXL 工业产销率-----------------------------------
        code_id = GMYSGY_CXL;
        onemap = new HashMap<>();
        data = indexService.getIndexData(code_id, datetime, "GX350200", "Y", "E");
        monthdata = savedatas(code_id, "GX350200", "Y", datetime.substring(0, 4), "E");
        ranks = indexService.getRank(code_id, datetime, "GX350200",
                "Y", areaType, "cumulative_value", true);//获取排名（增长值   含平潭）
        if (ObjectUtils.isNotBlank(ranks)) {
            rank = ranks.get(0).getRanks() + "";
        }
        onemap.put("monhtRatio", monthdata.get("list"));
        onemap.put("maxRatio", monthdata.get("max"));
        onemap.put("indexCode", code_id);
        onemap.put("areaType", areaType);
        onemap.put("codeType", "Y");
        onemap.put("indexName", "工业产销率");
        onemap.put("totalValue", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getCumulativeValue()) ? "0" : data.getCumulativeValue());
        onemap.put("ratio", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getCumulativeValue()) ? "0" : dataFormat(data.getCumulativeValue()));
        onemap.put("ranks", rank);
        onemap.put("datetime", datetime);
        onemap.put("areaCode", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getAreaCode()) ? "GX350200" : data.getAreaCode());
        onemap.put("totalValueUnit", "");    //单位

        if (StringUtils.isBlank(hideCodes.get(GMYSGY_CXL))) {// 没有权限就不让看了 A010101
            if (!"2019".equals(datetime.substring(0, 4))) {
                listmap.add(onemap);
            }
        }

        // ------------------------------------固定资产投资（不含农户） GDZCTZ-----------------------
        code_id = GDZCTZ;
        onemap = new HashMap<>();
        data = indexService.getIndexData(code_id, datetime, "GX350200", "Y", areaType);
        monthdata = savedatas(code_id, "GX350200", "Y", datetime.substring(0, 4), areaType);
        ranks = indexService.getRank(code_id, datetime, "GX350200",
                "Y", areaType, null, false);//获取排名（增长值   含平潭）
        if (ObjectUtils.isNotBlank(ranks)) {
            rank = ranks.get(0).getRanks() + "";
        }
        onemap.put("monhtRatio", monthdata.get("list"));
        onemap.put("maxRatio", monthdata.get("max"));
        onemap.put("indexCode", code_id);
        onemap.put("areaType", areaType);
        onemap.put("codeType", "Y");
        onemap.put("indexName", "固定资产投资额（不含农户）");
        onemap.put("totalValue", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getCumulativeValue()) ? "0" : data.getCumulativeValue());
        onemap.put("ratio", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getCumulativeRatio()) ? "0" : dataFormat1(data.getCumulativeRatio()));
        onemap.put("ranks", rank);
        onemap.put("datetime", datetime);
        onemap.put("areaCode", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getAreaCode()) ? "GX350200" : data.getAreaCode());
        onemap.put("totalValueUnit", "");    //单位

        if (StringUtils.isBlank(hideCodes.get(GDZCTZ))) {// 没有权限就不让看了 A010101
            listmap.add(onemap);
        }

        //------------------------------------社会消费品零售总额 SHXFPLSZE---------------------------
        code_id = SHXFPLSZE;
//        unitMap = Until.getUnit(YUEDU_9DI, code_id);
        onemap = new HashMap<>();
        data = indexService.getIndexData(code_id, datetime, "GX350200", "Y", areaType);
        monthdata = savedatas(code_id, "GX350200", "Y", datetime.substring(0, 4), areaType);
        ranks = indexService.getRank(code_id, datetime, "GX350200",
                "Y", areaType, null, true);//获取排名（增长值   含平潭）
        if (ObjectUtils.isNotBlank(ranks)) {
            rank = ranks.get(0).getRanks() + "";
        }
        onemap.put("monhtRatio", monthdata.get("list"));
        onemap.put("maxRatio", monthdata.get("max"));
        onemap.put("indexCode", code_id);
        onemap.put("areaType", areaType);
        onemap.put("codeType", "Y");
        onemap.put("indexName", "社会消费品零售总额");
        onemap.put("totalValue", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getCumulativeValue()) ? "0" :
                dataFormat(Double.parseDouble(kexuejishu(data.getCumulativeValue())) + ""));
        onemap.put("ratio", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getCumulativeRatio()) ? "0" : dataFormat1(data.getCumulativeRatio()));
        onemap.put("ranks", rank);
        onemap.put("datetime", datetime);
        onemap.put("areaCode", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getAreaCode()) ? "GX350200" : data.getAreaCode());
        onemap.put("totalValueUnit", "亿元");    //单位

        if (StringUtils.isBlank(hideCodes.get(SHXFPLSZE))) {// 没有权限就不让看了 A010101
            listmap.add(onemap);
        }

        //------------------------------------财政总收入 CZZSR------------------------------------
        /*code_id = CZZSR;
//        unitMap = Until.getUnit(YUEDU_9DI, code_id);
        onemap = new HashMap<>();
        data = indexService.getIndexData(code_id, datetime, "GX350200", "Y", areaType);
        monthdata = savedatas(code_id, "GX350200", "Y", datetime.substring(0, 4), areaType);
        ranks = indexService.getRank(code_id, datetime, "GX350200",
                "Y", areaType, null, true);//获取排名（增长值   含平潭）
        if (ObjectUtils.isNotBlank(ranks)) {
            rank = ranks.get(0).getRanks() + "";
        }
        onemap.put("monhtRatio", monthdata.get("list"));
        onemap.put("maxRatio", monthdata.get("max"));
        onemap.put("indexCode", code_id);
        onemap.put("areaType", areaType);
        onemap.put("codeType", "Y");
        onemap.put("indexName", "财政总收入");
        onemap.put("totalValue", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getCumulativeValue()) ? "0" :
                dataFormat(Double.parseDouble(kexuejishu(data.getCumulativeValue())) + ""));
        onemap.put("ratio", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getCumulativeRatio()) ? "0" : dataFormat1(data.getCumulativeRatio()));
        onemap.put("ranks", rank);
        onemap.put("datetime", datetime);
        onemap.put("areaCode", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getAreaCode()) ? "GX350200" : data.getAreaCode());
        onemap.put("totalValueUnit", "亿元");    //单位

        if (StringUtils.isBlank(hideCodes.get(CZZSR))) {// 没有权限就不让看了 A010101
            listmap.add(onemap);
        }*/

        //------------------------------------地方级财政收入  DFJCZSR------------------------------------
        code_id = DFJCZSR;
//        unitMap = Until.getUnit(YUEDU_9DI, code_id);
        onemap = new HashMap<>();
        data = indexService.getIndexData(code_id, datetime, "GX350200", "Y", areaType);
        monthdata = savedatas(code_id, "GX350200", "Y", datetime.substring(0, 4), areaType);
        ranks = indexService.getRank(code_id, datetime, "GX350200",
                "Y", areaType, null, true);//获取排名（增长值   含平潭）
        if (ObjectUtils.isNotBlank(ranks)) {
            rank = ranks.get(0).getRanks() + "";
        }
        onemap.put("monhtRatio", monthdata.get("list"));
        onemap.put("maxRatio", monthdata.get("max"));
        onemap.put("indexCode", code_id);
        onemap.put("areaType", areaType);
        onemap.put("codeType", "Y");
        onemap.put("indexName", "地方级财政收入");
        onemap.put("totalValue", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getCumulativeValue()) ? "0" :
                dataFormat(Double.parseDouble(data.getCumulativeValue()) + ""));
        onemap.put("ratio", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getCumulativeRatio()) ? "0" : dataFormat1(data.getCumulativeRatio()));
        onemap.put("ranks", rank);
        onemap.put("datetime", datetime);
        onemap.put("areaCode", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getAreaCode()) ? "GX350200" : data.getAreaCode());
        onemap.put("totalValueUnit", "亿元");    //单位
        if (StringUtils.isBlank(hideCodes.get(DFJCZSR))) {// 没有权限就不让看了
            listmap.add(onemap);
        }

        // ------------------------------------ 财政支出 CZZC------------------------------------
        code_id = CZZC;
//        unitMap = Until.getUnit(YUEDU_9DI, code_id);
        onemap = new HashMap<>();
        data = indexService.getIndexData(code_id, datetime, "GX350200", "Y", areaType);
        monthdata = savedatas(code_id, "GX350200", "Y", datetime.substring(0, 4), areaType);
        ranks = indexService.getRank(code_id, datetime, "GX350200",
                "Y", areaType, null, true);//获取排名（增长值   含平潭）
        if (ObjectUtils.isNotBlank(ranks)) {
            rank = ranks.get(0).getRanks() + "";
        }
        onemap.put("monhtRatio", monthdata.get("list"));
        onemap.put("maxRatio", monthdata.get("max"));
        onemap.put("indexCode", code_id);
        onemap.put("areaType", areaType);
        onemap.put("codeType", "Y");
        onemap.put("indexName", "财政支出");
        onemap.put("totalValue", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getCumulativeValue()) ? "0" :
                dataFormat(Double.parseDouble(data.getCumulativeValue()) + ""));
        onemap.put("ratio", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getCumulativeRatio()) ? "0" : dataFormat1(data.getCumulativeRatio()));
        onemap.put("ranks", rank);
        onemap.put("datetime", datetime);
        onemap.put("areaCode", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getAreaCode()) ? "GX350200" : data.getAreaCode());
        onemap.put("totalValueUnit", "亿元");    //单位
        if (StringUtils.isBlank(hideCodes.get(CZZC))) {// 没有权限就不让看了 A010101
            listmap.add(onemap);
        }

        //------------------------------------外贸进出口总额  WMJCKZE------------------------------------
        code_id = WMJCKZE;
//        unitMap = Until.getUnit(YUEDU_9DI, code_id);
        onemap = new HashMap<>();
        data = indexService.getIndexData(code_id, datetime, "GX350200", "Y", areaType);
        monthdata = savedatas(code_id, "GX350200", "Y", datetime.substring(0, 4), areaType);
        ranks = indexService.getRank(code_id, datetime, "GX350200",
                "Y", areaType, null, true);//获取排名（增长值   含平潭）
        if (ObjectUtils.isNotBlank(ranks)) {
            rank = ranks.get(0).getRanks() + "";
        }
        onemap.put("monhtRatio", monthdata.get("list"));
        onemap.put("maxRatio", monthdata.get("max"));
        onemap.put("indexCode", code_id);
        onemap.put("areaType", areaType);
        onemap.put("codeType", "Y");
        onemap.put("indexName", "外贸进出口总额");
        onemap.put("totalValue", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getCumulativeValue()) ? "0" :
                dataFormat(Double.parseDouble(data.getCumulativeValue()) + ""));
        onemap.put("ratio", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getCumulativeRatio()) ? "0" : dataFormat1(data.getCumulativeRatio()));
        onemap.put("ranks", rank);
        onemap.put("datetime", datetime);
        onemap.put("areaCode", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getAreaCode()) ? "GX350200" : data.getAreaCode());
        onemap.put("totalValueUnit", "亿元");    //单位
        if (StringUtils.isBlank(hideCodes.get(WMJCKZE))) {// 没有权限就不让看了
            listmap.add(onemap);
        }

        //------------------------------------出口总额  CKZE------------------------------------
        code_id = CKZE;
//        unitMap = Until.getUnit(YUEDU_9DI, code_id);
        onemap = new HashMap<>();
        data = indexService.getIndexData(code_id, datetime, "GX350200", "Y", areaType);
        monthdata = savedatas(code_id, "GX350200", "Y", datetime.substring(0, 4), areaType);
        ranks = indexService.getRank(code_id, datetime, "GX350200",
                "Y", areaType, null, true);//获取排名（增长值   含平潭）
        if (ObjectUtils.isNotBlank(ranks)) {
            rank = ranks.get(0).getRanks() + "";
        }
        onemap.put("monhtRatio", monthdata.get("list"));
        onemap.put("maxRatio", monthdata.get("max"));
        onemap.put("indexCode", code_id);
        onemap.put("areaType", areaType);
        onemap.put("codeType", "Y");
        onemap.put("indexName", "出口总额");
        onemap.put("totalValue", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getCumulativeValue()) ? "0" :
                dataFormat(Double.parseDouble(kexuejishu(data.getCumulativeValue())) + ""));
        onemap.put("ratio", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getCumulativeRatio()) ? "0" : dataFormat1(data.getCumulativeRatio()));
        onemap.put("ranks", rank);
        onemap.put("datetime", datetime);
        onemap.put("areaCode", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getAreaCode()) ? "GX350200" : data.getAreaCode());
        onemap.put("totalValueUnit", "亿元");    //单位
        if (StringUtils.isBlank(hideCodes.get(CKZE))) {// 没有权限就不让看了
            listmap.add(onemap);
        }

        //------------------------------------实际利用外资额  SJLYWZ------------------------------------
        code_id = SJLYWZ;
//        unitMap = Until.getUnit(YUEDU_9DI, code_id);
        onemap = new HashMap<>();
        data = indexService.getIndexData(code_id, datetime, "GX350200", "Y", areaType);
        monthdata = savedatas(code_id, "GX350200", "Y", datetime.substring(0, 4), "E");
        ranks = indexService.getRank(code_id, datetime, "GX350200",
                "Y", areaType, null, true);//获取排名（增长值   含平潭）
        if (ObjectUtils.isNotBlank(ranks)) {
            rank = ranks.get(0).getRanks() + "";
        }
        onemap.put("monhtRatio", monthdata.get("list"));
        onemap.put("maxRatio", monthdata.get("max"));
        onemap.put("indexCode", code_id);
        onemap.put("areaType", areaType);
        onemap.put("codeType", "Y");
        onemap.put("indexName", "实际利用外资额");
        onemap.put("totalValue", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getCumulativeValue()) ? "0" :
                dataFormat(Double.parseDouble(data.getCumulativeValue()) + ""));
        onemap.put("ratio", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getCumulativeRatio()) ? "0" : dataFormat1(data.getCumulativeRatio()));
        onemap.put("ranks", rank);
        onemap.put("datetime", datetime);
        onemap.put("areaCode", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getAreaCode()) ? "GX350200" : data.getAreaCode());
        onemap.put("totalValueUnit", "亿元");    //单位
        if (StringUtils.isBlank(hideCodes.get(SJLYWZ))) {// 没有权限就不让看了 A010101
            listmap.add(onemap);
        }

        //------------------------------------金融机构本外币各项存款余额  CKYE------------------------------------
        code_id = CKYE;
//        unitMap = Until.getUnit(YUEDU_9DI, code_id);
        onemap = new HashMap<>();
        data = indexService.getIndexData(code_id, datetime, "GX350200", "Y", "E");
        monthdata = savedatas(code_id, "GX350200", "Y", datetime.substring(0, 4), areaType);
        ranks = indexService.getRank(code_id, datetime, "GX350200",
                "Y", areaType, null, true);//获取排名（增长值   含平潭）
        if (ObjectUtils.isNotBlank(ranks)) {
            rank = ranks.get(0).getRanks() + "";
        }
        //如果累计值都为0，当前值的数据搬到累计值
        if ("0".equals(data.getCumulativeValue())) {
            data.setCumulativeValue(data.getCurrentValue());
        }
        onemap.put("monhtRatio", monthdata.get("list"));
        onemap.put("maxRatio", monthdata.get("max"));
        onemap.put("indexCode", code_id);
        onemap.put("areaType", areaType);
        onemap.put("codeType", "Y");
        onemap.put("indexName", "金融机构本外币各项存款余额");
        onemap.put("totalValue", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getCumulativeValue()) ? "0" : dataFormat(data.getCumulativeValue()));
        onemap.put("ratio", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getCumulativeRatio()) ? "0" : dataFormat1(data.getCumulativeRatio()));
        onemap.put("ranks", rank);
        onemap.put("datetime", datetime);
        onemap.put("areaCode", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getAreaCode()) ? "GX350200" : data.getAreaCode());
        onemap.put("totalValueUnit", "亿元");    //单位
        if (StringUtils.isBlank(hideCodes.get(CKYE))) {// 没有权限就不让看了 A010101
            listmap.add(onemap);
        }

        //------------------------------------金融机构本外币各项贷款余额 DKYE------------------------------------
        code_id = DKYE;
//        unitMap = Until.getUnit(YUEDU_9DI, code_id);
        onemap = new HashMap<>();
        data = indexService.getIndexData(code_id, datetime, "GX350200", "Y", "E");
        monthdata = savedatas(code_id, "GX350200", "Y", datetime.substring(0, 4), areaType);
        ranks = indexService.getRank(code_id, datetime, "GX350200",
                "Y", areaType, null, true);//获取排名（增长值   含平潭）
        if (ObjectUtils.isNotBlank(ranks)) {
            rank = ranks.get(0).getRanks() + "";
        }
        //如果累计值都为0，当前值的数据搬到累计值
        if ("0".equals(data.getCumulativeValue())) {
            data.setCumulativeValue(data.getCurrentValue());
        }
        onemap.put("monhtRatio", monthdata.get("list"));
        onemap.put("maxRatio", monthdata.get("max"));
        onemap.put("indexCode", code_id);
        onemap.put("areaType", areaType);
        onemap.put("codeType", "Y");
        onemap.put("indexName", "金融机构本外币各项贷款余额");
        onemap.put("totalValue", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getCumulativeValue()) ? "0" : dataFormat(data.getCumulativeValue()));
        onemap.put("ratio", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getCumulativeRatio()) ? "0" : dataFormat1(data.getCumulativeRatio()));
        onemap.put("ranks", rank);
        onemap.put("datetime", datetime);
        onemap.put("areaCode", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getAreaCode()) ? "GX350200" : data.getAreaCode());
        onemap.put("totalValueUnit", "亿元");    //单位
        if (StringUtils.isBlank(hideCodes.get(DKYE))) {// 没有权限就不让看了 A010101
            listmap.add(onemap);
        }

        //------------------------------------居民消费价格总指数 A0801------------------------------------
        /*code_id = "A0801";
        onemap = new HashMap<>();
        data = indexService.getIndexData(code_id, datetime, "GX350200", "Y", "E");
        monthdata = savedatas(code_id, "GX350200", "Y", datetime.substring(0, 4), "E");
        ranks = indexService.getRank(code_id, datetime, "GX350200",
                "Y", "E", null, true);//获取排名（增长值   含平潭）
        if (ObjectUtils.isNotBlank(ranks)) {
            rank = ranks.get(0).getRanks() + "";
        }
        onemap.put("monhtRatio", monthdata.get("list"));
        onemap.put("maxRatio", monthdata.get("max"));
        onemap.put("indexCode", code_id);
        onemap.put("areaType", areaType);
        onemap.put("indexName", "居民消费价格总指数");
        onemap.put("totalValue", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getCumulativeValue()) ? "0" : data.getCumulativeValue());
        onemap.put("ratio", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getCumulativeValue()) ? "0" : dataFormat1(data.getCumulativeValue()));
        onemap.put("ranks", rank);
        onemap.put("datetime", datetime);
        onemap.put("areaCode", ObjectUtils.isBlank(data) ? "0" : StringUtils.isBlank(data.getAreaCode()) ? "GX350200" : data.getAreaCode());
        onemap.put("totalValueUnit", eyMap.get(code_id));    //单位
        if (StringUtils.isBlank(hideCodes.get("A0801"))) {// 没有权限就不让看了 A010101
            listmap.add(onemap);
        }*/

        return listmap;
    }

    //处理数据
    public Map<String, Object> savedatas(String codeId, String areaCode, String codeType, String dateTime, String areaType) {
        String dateTime0 = Integer.parseInt(dateTime) - 1 + "";
        List<IndexData> indexdata1 = indexService.getListIndexDate(codeId, areaCode, codeType, dateTime0, areaType);
        List<IndexData> indexdata2 = indexService.getListIndexDate(codeId, areaCode, codeType, dateTime, areaType);
        indexdata1.addAll(indexdata2);
        List<IndexData> indexdatas = new ArrayList<>();
        for (IndexData i : indexdata1) {
            if (i.getDatetime().length() == 6) {
                if ("01".equals(i.getDatetime().substring(4, 6))) {
                    if (StringUtils.isBlank(i.getCumulativeRatio()) || "0".equals(i.getCumulativeRatio()))
                        continue;
                }
            }
            indexdatas.add(i);
        }
        List<IndexDto> dtos = new ArrayList<>();
        for (IndexData item : indexdatas) {
            IndexDto dto = new IndexDto();
            dto.setDatatime(item.getDatetime());
            //工业产销率
//            if ("A020201".equals(item.getCodeId()) && "Y".equals(item.getCodeType()) && "E".equals(item.getAreaType())) {
//                dto.setRatio(StringUtils.isBlank(item.getCumulativeValue()) ? "0" : dataFormat(item.getCumulativeValue()));
//            } else {
//                dto.setRatio(StringUtils.isBlank(item.getCumulativeRatio()) ? "0" : dataFormat(item.getCumulativeRatio()));
//            }
            dto.setRatio(StringUtils.isBlank(item.getCumulativeRatio()) ? "0" : dataFormat(item.getCumulativeRatio()));

            if (!"0".equals(dto.getRatio())) {
                dtos.add(dto);
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("list", dtos);
        List<IndexDto> max = new ArrayList<>(dtos);
        if (ObjectUtils.isNotBlank(max)) {
            Collections.sort(max, new Comparator<IndexDto>() {
                @Override
                public int compare(IndexDto o1, IndexDto o2) {
                    if (StringUtils.isNotBlank(o1.getRatio()) && StringUtils.isNotBlank(o2.getRatio())) {
                        if (Double.parseDouble(o1.getRatio()) < Double.parseDouble(o2.getRatio())) {
                            return 1;
                        } else if (Double.parseDouble(o1.getRatio()) == Double.parseDouble(o2.getRatio())) {
                            return 0;
                        } else {
                            return -1;
                        }
                    }
                    return 0;
                }
            });
        }
        map.put("max", ObjectUtils.isBlank(max) ? "0" : max.get(0).getRatio());
        return map;
    }


    /**
     * 月度 季度，年度 目录
     *
     * @param codeNode
     * @param codeType
     * @param areaType
     * @return
     */
    public List<Map<String, Object>> getCityView(String codeNode, String codeType, String areaType, UserInfo userInfo) {
        String codeTypeParam = codeType;
        String areaCode = "GX350200";
        if ("G".equals(areaType)) {
            areaCode = "GX000000";
        }

        /**一级指标**/
        List<Map<String, Object>> list1 = new ArrayList<>();
        //调整：旧版年度指标为月度指标，其数据为月度的12月份的累计； 之前的年度数据实则为年鉴数据
        if ("T".equals(codeType)) {
            codeType = "Y";
        }
        //获取一级目标指标
        List<OneIndex> oneindes = oneservice.getTwoNode(codeType, "1", areaType);
        List<OneIndex> allOneindes;
        List<OneIndex> jsonList = new ArrayList<>();
        if ("N".equals(codeType) && "D".equals(areaType)) {
            //从临时表中获取所有目标指标
            //判断redis里面是否有缓存
            String flag = redis.get("allIndexRedis");
            if (StringUtils.isNotBlank(flag)) {
                allOneindes = Str2List(flag, OneIndex.class);
            } else {
                allOneindes = oneservice.getIndexFromTemp();
                String resStr = List2Str(allOneindes);
                redis.set("allIndexRedis", resStr);      //数据缓存到redis
            }
        } else {
            //获取所有目标指标
            allOneindes = oneservice.getTwoNode(codeType, null, areaType);
        }


        Collections.sort(allOneindes, new Comparator<OneIndex>() {
            @Override
            public int compare(OneIndex o1, OneIndex o2) {
                // 降序排序
                return o1.getCodeId().compareTo(o2.getCodeId());
            }
        });


        List<Map<String, OneIndex>> oneNodeList = new ArrayList<>();
        List<Map<String, OneIndex>> twoNodeList = new ArrayList<>();
        List<Map<String, OneIndex>> threeNodeList = new ArrayList<>();
        List<Map<String, OneIndex>> fourNodeList = new ArrayList<>();

        for (OneIndex a : allOneindes) {
            Map<String, OneIndex> indexMap = new HashMap<>();
            if ("2".equals(a.getCodeNode())) {
                indexMap.put(a.getParentCode(), a);
                oneNodeList.add(indexMap);
            }
            if ("3".equals(a.getCodeNode())) {
                indexMap.put(a.getParentCode(), a);
                twoNodeList.add(indexMap);
            }
            if ("4".equals(a.getCodeNode())) {
                indexMap.put(a.getParentCode(), a);
                threeNodeList.add(indexMap);
            }
            if ("5".equals(a.getCodeNode())) {
                indexMap.put(a.getParentCode(), a);
                fourNodeList.add(indexMap);
            }
        }


        Map<String, String> hideCodes = userPressService.getHideCodeList(userInfo.getSysId());
        for (OneIndex oneItem : oneindes) {
            if (StringUtils.isNotBlank(hideCodes.get(oneItem.getCodeId()))) {// 没有权限就不让看了
                continue;
            }
            Map<String, Object> map1 = new HashMap<>();
            map1.put("codeId", oneItem.getCodeId());
            map1.put("codeName", oneItem.getCodeName());
            map1.put("areaCode", areaCode);
            //map1.put("codeNode",1);
            /**二级指标**/
            List<Map<String, Object>> list2 = new ArrayList<>();

            List<OneIndex> index2 = new ArrayList<>();
            for (Map<String, OneIndex> map : oneNodeList) {
                if (map.containsKey(oneItem.getCodeId())) {
                    //年度的【--201802这个】这种指标的不展示
                    /*if ("T".equals(codeTypeParam)) {
                        OneIndex tempIndex = map.get(oneItem.getCodeId());
                        if (tempIndex.getCodeName().contains("（-20") || tempIndex.getCodeName().contains("-201")) {
                            continue;
                        }
                    }*/
                    //所有的 【--201802这个】这种指标的不展示
                    OneIndex tempIndex = map.get(oneItem.getCodeId());
                    if (tempIndex.getCodeName().contains("（-20") || tempIndex.getCodeName().contains("-201")) {
                        continue;
                    }
                    index2.add(map.get(oneItem.getCodeId()));
                }
            }
            for (OneIndex item2 : index2) {
                if (StringUtils.isNotBlank(hideCodes.get(item2.getCodeId()))) {// 没有权限就不让看了
                    continue;
                }
                Map<String, Object> map2 = new HashMap<>();
                map2.put("codeId", item2.getCodeId());
                map2.put("codeName", item2.getCodeName());
                map2.put("areaCode", areaCode);
                //map1.put("codeNode",2);
                /**三级指标**/
                List<Map<String, Object>> list3 = new ArrayList<>();

                List<OneIndex> index3 = new ArrayList<>();
                for (Map<String, OneIndex> map : twoNodeList) {
                    if (map.containsKey(item2.getCodeId())) {
                        index3.add(map.get(item2.getCodeId()));
                    }
                }

                for (OneIndex item3 : index3) {
                    if (StringUtils.isNotBlank(hideCodes.get(item3.getCodeId()))) {// 没有权限就不让看了
                        continue;
                    }
                    Map<String, Object> map3 = new HashMap<>();
                    map3.put("codeId", item3.getCodeId());
                    map3.put("codeName", item3.getCodeName());
                    map3.put("areaCode", areaCode);
                    //map1.put("codeNode",3);
                    /**四级指标**/
                    List<Map<String, Object>> list4 = new ArrayList<>();

                    List<OneIndex> index4 = new ArrayList<>();
                    for (Map<String, OneIndex> map : threeNodeList) {
                        if (map.containsKey(item3.getCodeId())) {
                            index4.add(map.get(item3.getCodeId()));
                        }
                    }
                    for (OneIndex item4 : index4) {
                        if (StringUtils.isNotBlank(hideCodes.get(item4.getCodeId()))) {// 没有权限就不让看了
                            continue;
                        }
                        Map<String, Object> map4 = new HashMap<>();
                        map4.put("codeId", item4.getCodeId());
                        map4.put("codeName", item4.getCodeName());
                        map4.put("areaCode", areaCode);
                        if ("A0C010101".equals(item4.getCodeId())) {
                            String abc = "abc";
                        }
                        /**五级指标**/
                        List<Map<String, Object>> list5 = new ArrayList<>();

                        List<OneIndex> index5 = new ArrayList<>();
                        for (Map<String, OneIndex> map : fourNodeList) {
                            if (map.containsKey(item4.getCodeId())) {
                                index5.add(map.get(item4.getCodeId()));
                            }
                        }
                        for (OneIndex item5 : index5) {
                            if (StringUtils.isNotBlank(hideCodes.get(item5.getCodeId()))) {// 没有权限就不让看了
                                continue;
                            }
                            Map<String, Object> map5 = new HashMap<>();
                            map5.put("codeId", item5.getCodeId());
                            map5.put("codeName", item5.getCodeName());
                            map5.put("areaCode", areaCode);
                            list5.add(map5);
                        }
                        map4.put("childData", list5);
                        list4.add(map4);
                        if (index5.size() > 0 && list5.size() == 0) {
                            list4.remove(map4);
                        }
                    }
                    map3.put("childData", list4);
                    list3.add(map3);
                    if (index4.size() > 0 && list4.size() == 0) {
                        list3.remove(map3);
                    }
                }
                map2.put("childData", list3);
                list2.add(map2);
                if (index3.size() > 0 && list3.size() == 0) {
                    list2.remove(map2);
                }
            }
            map1.put("childData", list2);
            list1.add(map1);
            if (index2.size() > 0 && list2.size() == 0) {
                list1.remove(map1);
            }
        }
        //全国月度，把价格指数挪到最下面
        if ("G".equals(areaType) && "Y".equals(codeType)) {
            list1.add(list1.get(0));
            list1.remove(0);
        }
        return list1;
    }

    /**
     * @param keyword
     * @param
     * @return
     */
    public List<IndexDataDTO> getSearcheData(String keyword) {

        String area = null;

        /* if(keyword.contains())*/
        String[] codetype = {"Y", "J", "N"};
        List<IndexDataDTO> lists = new ArrayList<>();
        for (int i = 0; i < codetype.length; i++) {
            List<IndexDataDTO> dto = indexService.getSearchData(keyword, null, null, codetype[i]);
            lists.addAll(dto);
        }

        for (int i = 0; i < codetype.length; i++) {
            List<IndexDataDTO> dto = indexService.getSearchData(null, keyword, null, codetype[i]);
            lists.addAll(dto);
        }

        for (int i = 0; i < codetype.length; i++) {
            List<IndexDataDTO> dto = indexService.getSearchData(null, null, keyword, codetype[i]);
            lists.addAll(dto);
        }

        // List<IndexDataDTO>  dto = indexService.getSearchData(null,keyword,null,"Y");

        System.out.println(lists);


        return lists;
    }


    public boolean checkQuarterTime(String datetime) {
        if ("03".equals(datetime.substring(4, 6)) ||
                "06".equals(datetime.substring(4, 6)) ||
                "09".equals(datetime.substring(4, 6)) ||
                "12".equals(datetime.substring(4, 6))) {
            return true;
        } else {
            return false;
        }
    }

    public String dealWithQuarterTime(String datetime) {
        String year = datetime.substring(0, 4);
        String flag = datetime.substring(4, 6);
        int yearInt = Integer.parseInt(year);
        String result = "";

        if ("01".equals(flag) || "02".equals(flag)) {
            result = yearInt - 1 + "" + "D";
        }
        if ("03".equals(flag) || "04".equals(flag) || "05".equals(flag)) {
            result = year + "A";
        }
        if ("06".equals(flag) || "07".equals(flag) || "08".equals(flag)) {
            result = year + "B";
        }
        if ("09".equals(flag) || "10".equals(flag) || "11".equals(flag)) {
            result = year + "C";
        }
        if ("12".equals(flag)) {
            result = year + "D";
        }
        return result;
    }


    public String quarterTitleNameByTime(String datetime) {
        String flag = datetime.substring(4, 6);
        String titleName = "";
        if ("01".equals(flag) || "02".equals(flag)) {
            titleName = "（上年第四季度）";
        }
        if ("03".equals(flag) || "04".equals(flag) || "05".equals(flag)) {
            titleName = "（第一季度）";
        }
        if ("06".equals(flag) || "07".equals(flag) || "08".equals(flag)) {
            titleName = "（第二季度）";
        }
        if ("09".equals(flag) || "10".equals(flag) || "11".equals(flag)) {
            titleName = "（第三季度）";
        }
        if ("12".equals(flag)) {
            titleName = "（第四季度）";
        }
        return titleName;
    }

}
